﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHealthOnline.Business;

namespace MyHealthOnline.Administrator
{
    public partial class ManageSpecialists : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            SpecialistBusiness.CreateSpecialist(txtNewName.Text, txtNewPassword.Text, txtNewCode.Text,
                            txtNewOrganization.Text, ddlNewDisciplines.SelectedValue, txtNewEmail.Text,
                            txtNewAddress.Text, txtNewCity.Text, txtNewCountry.Text, txtNewPhone.Text, txtNewBirthday.Text,
                            rblGender.SelectedValue);
            grdSpecialists.DataBind();

            txtNewName.Text = "";
            txtNewPassword.Text = "";
            txtNewCity.Text = "";
            txtNewOrganization.Text = "";
            ddlNewDisciplines.ClearSelection();
            txtNewEmail.Text = "";
            txtNewAddress.Text = "";
            txtNewCity.Text = "";
            txtNewCountry.Text = "";
            txtNewPhone.Text = "";
            txtNewBirthday.Text = "";
            rblGender.ClearSelection();

            //updSpecialists.Update();

            WebServiceHO webService = new WebServiceHO();
            webService.Notify("administrator@webconsult.gmail", txtNewEmail.Text,
                "Your account on web consult is created. You have the password " + txtNewPassword.Text,
                "Account created");
        }

        protected void grdSpecialists_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "Select")
            {
                //int id = Convert.ToInt32(grdPatients.DataKeys[index].Value);
                grdSpecialists.EditIndex = index;
            }
            else if (e.CommandName == "Edit")
            {
                grdSpecialists.EditIndex = index;
            }
            else if (e.CommandName == "Update")
            {
                TextBox txtName = (TextBox)grdSpecialists.Rows[index].FindControl("txtName");
                TextBox txtPassword = (TextBox)grdSpecialists.Rows[index].FindControl("txtPassword");
                TextBox txtNumber = (TextBox)grdSpecialists.Rows[index].FindControl("txtNumber");
                TextBox txtEmail = (TextBox)grdSpecialists.Rows[index].FindControl("txtEmail");
                TextBox txtAddress = (TextBox)grdSpecialists.Rows[index].FindControl("txtAddress");
                TextBox txtCity = (TextBox)grdSpecialists.Rows[index].FindControl("txtCity");
                TextBox txtCountry = (TextBox)grdSpecialists.Rows[index].FindControl("txtCountry");
                TextBox txtPhone = (TextBox)grdSpecialists.Rows[index].FindControl("txtPhone");
                TextBox txtBirthday = (TextBox)grdSpecialists.Rows[index].FindControl("txtBirthday");
                TextBox txtSex = (TextBox)grdSpecialists.Rows[index].FindControl("txtSex");
                DropDownList ddlDisciplines = (DropDownList)grdSpecialists.Rows[index].FindControl("ddlDisciplines");
                string id =grdSpecialists.DataKeys[index].Value.ToString();

                SpecialistBusiness.UpdateSpecialist(id, txtName.Text, txtPassword.Text, 
                            txtNewOrganization.Text, ddlNewDisciplines.SelectedValue, txtEmail.Text,
                          txtAddress.Text, txtCity.Text, txtCountry.Text, txtPhone.Text, txtBirthday.Text,txtSex.Text,
                          Convert.ToInt32( ddlNewDisciplines.SelectedValue));

                grdSpecialists.EditIndex = -1;
                grdSpecialists.DataBind();

                WebServiceHO webService = new WebServiceHO();
                webService.Notify("administrator@webconsult.gmail", txtEmail.Text,
                    "Your account on web consult is updated. You have the password " + txtPassword.Text,
                    "Account updated");
                //updSpecialists.Update();
            }
            else if (e.CommandName == "Cancel")
            {
                grdSpecialists.EditIndex = -1;
            }
            else if (e.CommandName == "Delete")
            {
                string id =grdSpecialists.DataKeys[index].Value.ToString();
                SpecialistBusiness.DeleteSpecialist(id);
                grdSpecialists.DataBind();
                //updSpecialists.Update();
            }
        }

        protected void grdSpecialists_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            grdSpecialists.EditIndex = -1;
            grdSpecialists.DataBind();
            //updSpecialists.Update();
        }
    }
}